#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
    if (argc < 3)
    {
        fprintf(stderr, "Usage\n");
        exit(1);
    }

    int sockfd;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        perror("socket()");
        exit(1);
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    int ret;
    ret = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
    if (ret < 0)
    {
        perror("connect()");
        exit(1);
    } 
  
    FILE *fp = fdopen(sockfd, "r+");
    if (fp == NULL)
    {
        perror("fopen()");
        exit(1);
    }
    
    while (1)
    {
//        send(sockfd, "hello\n", 7, 0);
        fprintf(fp, "%s\n", "hello");
        fflush(fp);
        sleep(1);
    }
    fclose(fp);
}
